/*! \file InputLayout.cpp
 *	\brief 
 *
 *	created:	2010/12/08
 *	created:	8:12:2010   15:46
 *	filename: 	C:\src\tactics-engine\libs\gd_dx11\InputLayout.cpp
 *	file path:	C:\src\tactics-engine\libs\gd_dx11
 *	file base:	InputLayout
 *	file ext:	cpp
 *	author:		Vitalii Trubchaninov
 *				vitalii.trubchaninov@ubisoft.com
 */

#include "gd_dx11/stdafx.h"
#include "gd_dx11/InputLayout.h"

#include <gd/InputLayoutDesc.h>

#include "gd_dx11/StringConversions.h"

namespace tt{
namespace gd_dx11{

InputLayout::InputLayout()
{

}

bool InputLayout::Create( const gd::InputLayoutDesc& desc )
{
	m_elements.resize( desc.elements().size() );

	UInt currentOffset = 0;
	InputLayoutElements::iterator element_it = m_elements.begin();
	for(gd::InputLayoutDescElements::const_iterator element_desc_it = desc.elements().begin(); element_desc_it != desc.elements().end(); ++element_desc_it, ++element_it)
	{
		const gd::InputLayoutDescElement& element_desc = *element_desc_it;
		D3D11_INPUT_ELEMENT_DESC& element = *element_it;

		element.SemanticName = element_desc.semantic.c_str();
		element.Format = FromString(element_desc.format.c_str());
		element.AlignedByteOffset = currentOffset;
		element.SemanticIndex = 0;
		element.InputSlot = 0;
		element.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
		element.InstanceDataStepRate = 0;

		currentOffset += FormatSize(element.Format);
	}

	return true;
}

const D3D11_INPUT_ELEMENT_DESC* InputLayout::data() const
{
	return &m_elements.front();
}

UInt InputLayout::size() const
{
	return (UInt)m_elements.size();
}

//////////////////////////////////////////////////////////////////////////
UInt InputLayout::FormatSize( DXGI_FORMAT format )
{
	switch(format)
	{
	case DXGI_FORMAT_R32G32B32_FLOAT:
		return 12;
	case DXGI_FORMAT_R32G32B32A32_FLOAT:
		return 16;
	};
	TT_UNREACHABLE("DXGI_FORMAT unsupported!");
}

} //namespace gd_dx11
} //namespace tt